﻿#include "AddressBookDelegate.h"
#include <QPainter>
#include "AddressBook.h"
#include <QListView>
#include "AddressBookHelper.h"
#include <QDebug>
#include <QEvent>
#include <QSvgRenderer>
#include <QMouseEvent>

AddressBookDelegate::AddressBookDelegate(QListView *pView, QObject *parent)
{
    m_pView = pView;
}

void AddressBookDelegate::paint(QPainter *painter,
                                const QStyleOptionViewItem &option,
                                const QModelIndex &index) const
{
    if(index.isValid())
    {
        painter->save();
        QFont font = painter->font();
        font.setPixelSize(11);
        font.setFamily("Microsoft YaHei");
        painter->setFont(font);

        QRect backRect = option.rect;
        QRectF rect1(backRect.left(), backRect.top(), backRect.width(), backRect.height() / 3.0);
        QRectF rect2(rect1.left(), rect1.bottom(), rect1.width(), rect1.height());
        QRectF rect3(rect2.left(), rect2.bottom(), rect2.width(), rect2.height());

        QVariant value = index.data(Qt::UserRole + 100);
        if(value.isValid())
        {
            const MemberInfo *pInfo = value.value<const MemberInfo*>();
            if(pInfo != nullptr)
            {
                // 绘制字母索引
                painter->drawText(rect1, Qt::AlignVCenter, pInfo->strNamePinYin.at(0).toUpper());

                // 绘制选择框
                {
                    QSvgRenderer render;
                    if(pInfo->bChecked)
                    {
                        render.load(QString("./RectCheckBoxDown.svg"));
                    }
                    else
                    {
                        render.load(QString("./RectCheckBoxUp.svg"));
                    }

                    QRectF boxRect(0,0, 10, 10);
                    boxRect.moveCenter(QPointF(rect2.left() + 5, rect2.top() + rect2.height() / 2.0));

                    render.render(painter, boxRect);
                }


                // 绘制名称
                painter->drawText(rect2.left() + 10 + 17, rect2.top(),
                                  rect2.width() - 10 - 17, rect2.height(),
                                  Qt::AlignVCenter | Qt::AlignLeft, pInfo->strName);

                // 绘制邮箱
                painter->drawText(rect3.left() + 10 + 17, rect3.top(),
                                  rect3.width() - 10 - 17, rect3.height(),
                                  Qt::AlignLeft | Qt::AlignVCenter, pInfo->strEmail);

                // 绘制分割线
                {
                    painter->save();
                    QColor color;
                    color.setNamedColor("#F0F0F0");
                    painter->setPen(color);
                    painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
                    painter->restore();
                }

            }
        }
        painter->restore();
    }
}

QSize AddressBookDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(m_pView == nullptr)
    {
        return QSize(0,0);
    }

    return QSize(m_pView->width()-10, 50);
}

bool AddressBookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        QRect backRect = option.rect;
        QRectF rect(backRect.left(), backRect.top() + backRect.height() / 3.0, backRect.width(), backRect.height() / 3.0);
        QRectF boxRect(0,0, 10, 10);
        boxRect.moveCenter(QPointF(rect.left() + 5, rect.top() + rect.height() / 2.0));

        QMouseEvent* mEvent = dynamic_cast<QMouseEvent*>(event);
        if(mEvent)
        {
            if(boxRect.contains(mEvent->pos()))
            {
                model->setData(index, true, Qt::UserRole + 101);
            }
        }
    }
    return true;
}

